from rest_framework.generics import ListAPIView,CreateAPIView
from .user_serializers import UserSerilizers
from users.models import User
from meiduo_admin.my_paginate import MyPageNuberPagination

class UserView(ListAPIView,CreateAPIView):
    pagination_class=MyPageNuberPagination
    #1.创建公用序列化器
    serializer_class = UserSerilizers
    #2.创建公共数据源
    # queryset=User.objects.all()
    #2.1重写数据原
    def get_queryset(self):
        # 1,获取查询关键字
        # query_params是request里的方法,它返回的是self._request.GET
        # 等同于request.GET.get
        keyword=self.request.query_params.get('keyword')
        # 2,判断是否有关键字,如果有关键字有将username里边包含有该关键字的用户返回去,也就是前端的需要的查找功能
        if keyword:
            return User.objects.filter(username__contains=keyword).all()
        else:
            return User.objects.order_by("id").all()


